/*-
 * Copyright 2012 Guram Dukashvili
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
//---------------------------------------------------------------------------
#ifndef __str_h__
#define __str_h__
//---------------------------------------------------------------------------
#include "config.h"
//---------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------
typedef struct string {
  char * s;
} string;
//---------------------------------------------------------------------------
string * string_new(void);
string * string_new_copy(const char * src,uintptr_t l);
void string_init(string * s);
void string_delete(string * s);
void string_destroy(string * s);
void string_copy_c(string * s,const char * src,uintptr_t l);
void string_copy(string * d,const string * s);
void string_cat_c(string * d,const string * s1,const char * s2,uintptr_t l);
void string_cat(string * d,const string * s1,const string * s2);
void string_vcat_c(string * d, /* const char * s1 */ ...);
void string_vcat(string * d, /* const string * s1 */ ...);
void string_vprint(string * s,const char * fmt,va_list args);
void string_print(string * s,const char * fmt, ... );
//---------------------------------------------------------------------------
uint32_t string_hash(const string * s,uint32_t h /*= 5381*/);
//---------------------------------------------------------------------------
intptr_t string_compare(const string * s1,const string * s2);
intptr_t string_icompare(const string * s1,const string * s2);
void string_xchg(string * s1,string * s2);
//---------------------------------------------------------------------------
typedef struct {
  string * s;
  uintptr_t position;    // in symbols
  uintptr_t cursor;     // in bytes
} string_it;
//---------------------------------------------------------------------------
string_it * string_it_new(string * s);
string_it * string_it_new_copy(const string_it * it);
void string_it_init(string_it * it,string * s);
void string_it_init_copy(string_it * it,const string_it * src);
void string_it_delete(string_it * it);
void string_it_destroy(string_it * it);
void string_it_prev(string_it * it);
void string_it_next(string_it * it);
void string_it_rewind(string_it * it);
void string_it_end(string_it * it);
void string_it_move_position(string_it * it,intptr_t syms);
int string_it_getc(const string_it * it);
//---------------------------------------------------------------------------
static __inline__ bool string_it_bos(const string_it * it)
{
  return it->cursor == 0;
}
//---------------------------------------------------------------------------
static __inline__ bool string_it_eos(const string_it * it)
{
  return it->s->s[it->cursor] == '\0';
}
//---------------------------------------------------------------------------
#if _MSC_VER
void string_ansi2oem(string * s);
#else
#define string_ansi2oem(s)
#endif
//---------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
////---------------------------------------------------------------------------
#endif // __str_h__
//---------------------------------------------------------------------------
